/**
  ******************************************************************************
  * File Name          : dwkcd.c
  * Description        : Code for dw lcd applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "tskcfg.h"
#include "dwlcd.h"
#include "collector.h"
#include "ctrl_common.h"
#include "storage.h"
#include <stdio.h>
#include <string.h>

TaskHandle_t xDwLcdRx_Thd = NULL;
StaticTask_t xDwLcdRx_Tcb;
StackType_t  xDwLcdRx_Stk[ DWLCDRX_STACK_SIZE ];

TaskHandle_t xDwLcdTx_Thd = NULL;
StaticTask_t xDwLcdTx_Tcb;
StackType_t  xDwLcdTx_Stk[ DWLCDTX_STACK_SIZE ];

QueueHandle_t Dw_Rx_Qhd = NULL;
static StaticQueue_t dw_rx_stru;
static uint8_t       dw_rx_area[DWQUE_RX_LENGTH * DWQUE_RX_ITEMSIZE];

QueueHandle_t Dw_Tx_Qhd = NULL;
static StaticQueue_t dw_tx_stru;
static uint8_t       dw_tx_area[DWQUE_TX_LENGTH * DWQUE_TX_ITEMSIZE];

static Dw_RxBufType dw_rxbuf_back;
static Dw_TxBufType dw_txbuf_back;

const uint8_t dwlcd_get_dev[7]= {HEADER1, HEADER2, 0x04, 0x83, 0x11, 0x00, 0x07};
const uint8_t dwlcd_get_jt[7] = {HEADER1, HEADER2, 0x04, 0x83, 0x40, 0x00, 0x0E};
const uint8_t dwlcd_get_dl[7] = {HEADER1, HEADER2, 0x04, 0x83, 0x50, 0x00, 0x18};
const uint8_t dwlcd_get_sk[7] = {HEADER1, HEADER2, 0x04, 0x83, 0x60, 0x01, 0x2F};
const uint8_t dwlcd_get_td[7] = {HEADER1, HEADER2, 0x04, 0x83, 0x21, 0x00, 0x17};
/********************************************************************************/
/* 用于屏幕临时设置保存参数的 */
static Ctrl_ParaMDType  Ctrl_SetMD;
static Ctrl_ParaJTType  Ctrl_SetJT;
static Ctrl_ParaDL1Type Ctrl_SetDL1;
static Ctrl_ParaDL2Type Ctrl_SetDL2;
static Ctrl_ParaSKType  Ctrl_SetSK;
static Ctrl_ParaTDType  Ctrl_SetTD;
/********************************************************************************/


static void dwlcd_init(void)
{
	Dw_Rx_Qhd = xQueueCreateStatic(DWQUE_RX_LENGTH, DWQUE_RX_ITEMSIZE, dw_rx_area, &dw_rx_stru);
	Dw_Tx_Qhd = xQueueCreateStatic(DWQUE_TX_LENGTH, DWQUE_TX_ITEMSIZE, dw_tx_area, &dw_tx_stru);
	
	Lcd_BspInit();
}

static void unpack_devsync(void)
{
  DW_Send_devlist();
  DW_Send_changeid();
}

static void unpack_sync(void)
{
  DW_Send_ec_mode();
  DW_Send_ec_para_jt();
  DW_Send_ec_para_dl();
  DW_Send_ec_para_sk();
  DW_Send_ec_para_td();
  DW_Send_rilcl();
  DW_Send_devinfo();
  DW_Send_devlist();
}

static void unpack_devinfo(void)
{
  Storage_SaveType sftype;
  char cmdbuf[TTY_RXBUF_MAXLEN];
  
  vTaskSuspend(xDwLcdTx_Thd);
  
  xQueueReset(Dw_Rx_Qhd);
  
  Lcd_UartSendBuf(dwlcd_get_dev, 7);
  
  if(xQueueReceive(Dw_Rx_Qhd, &dw_rxbuf_back, 5000) == pdTRUE)
  {
    if((dw_rxbuf_back.dat[0] == dwlcd_get_dev[4]) && \
       (dw_rxbuf_back.dat[2] == dwlcd_get_dev[6]) && \
       (dw_rxbuf_back.len == ((dwlcd_get_dev[6]*2)+3)))
    {
      if((dw_rxbuf_back.dat[4] > 0) && (dw_rxbuf_back.dat[4] < 9) && (SysCfg_RunPara.lora_num != dw_rxbuf_back.dat[4]))
      {
        SysCfg_RunPara.lora_num = dw_rxbuf_back.dat[4];
    
        if(Storage_qhd != NULL)
        {
          sftype = SFLASH_SAVE_SYSCFG;
          xQueueSend(Storage_qhd, &sftype, 0);
        }
      }
      
      dw_rxbuf_back.dat[16] = 0;
      cmdbuf[0] = 'I';
      cmdbuf[1] = 'D';
      cmdbuf[2] = ':';
      cmdbuf[3] = 'H';
      cmdbuf[4] = 'Z';
      strncpy(cmdbuf+5, (const char *)(dw_rxbuf_back.dat+5), TTY_RXBUF_MAXLEN-5);
      
      if(TTYRecv_qhd != NULL)
      {
        xQueueSend(TTYRecv_qhd, cmdbuf, 0);
      }
    }
  }
  
  vTaskResume(xDwLcdTx_Thd);
}

static void unpack_md(uint16_t dat)
{
  Storage_SaveType sftype;
  
  Ctrl_SetMD = Ctrl_ParaMD;
  
  switch(dat)
  {
    case 1:
      Ctrl_SetMD.mode = CTRL_MODE_JT;
      break;
    
    case 2:
      Ctrl_SetMD.mode = CTRL_MODE_DL;
      break;
    
    case 3:
      Ctrl_SetMD.mode = CTRL_MODE_SK;
      break;
    
    case 4:
      Ctrl_SetMD.mode = CTRL_MODE_ZN;
      break;
    
    case 5:
      Ctrl_SetMD.mode = CTRL_MODE_MU;
      break;
    
    default:
      return;
  }
  
  if(memcmp(&Ctrl_SetMD, &Ctrl_ParaMD, sizeof(Ctrl_ParaMDType)) != 0)
  {
    if(Ctr_CheckMDPara(&Ctrl_SetMD) != 0)
    {
      return;
    }
    
    Ctrl_ParaMD = Ctrl_SetMD;
    
    Route_ec_tonet_md();
    
    sftype = SFLASH_SAVE_CTRMD;
		if(Storage_qhd != NULL)
		{
			xQueueSend(Storage_qhd, &sftype, 0);
		}
  }
}

static void unpack_jt(void)
{
  Storage_SaveType sftype;
  
  vTaskSuspend(xDwLcdTx_Thd);
  
  xQueueReset(Dw_Rx_Qhd);
  
  Lcd_UartSendBuf(dwlcd_get_jt, 7);
  
  if(xQueueReceive(Dw_Rx_Qhd, &dw_rxbuf_back, 5000) == pdTRUE)
  {
    if((dw_rxbuf_back.dat[0] == dwlcd_get_jt[4]) && \
       (dw_rxbuf_back.dat[2] == dwlcd_get_jt[6]) && \
       (dw_rxbuf_back.len == ((dwlcd_get_jt[6]*2)+3)))
    {
      Ctrl_SetJT = Ctrl_ParaJT;
      
      Ctrl_SetJT.slhitp = ((((uint16_t)dw_rxbuf_back.dat[3]) << 8) & 0xff00u) + dw_rxbuf_back.dat[4];
      Ctrl_SetJT.sllotp = ((((uint16_t)dw_rxbuf_back.dat[5]) << 8) & 0xff00u) + dw_rxbuf_back.dat[6];
      Ctrl_SetJT.slrttp = ((((uint16_t)dw_rxbuf_back.dat[7]) << 8) & 0xff00u) + dw_rxbuf_back.dat[8];
      
      Ctrl_SetJT.fnhin3 = ((((uint16_t)dw_rxbuf_back.dat[9]) << 8) & 0xff00u) + dw_rxbuf_back.dat[10];
      Ctrl_SetJT.fnhitp = ((((uint16_t)dw_rxbuf_back.dat[11]) << 8) & 0xff00u) + dw_rxbuf_back.dat[12];
      Ctrl_SetJT.fnlotp = ((((uint16_t)dw_rxbuf_back.dat[13]) << 8) & 0xff00u) + dw_rxbuf_back.dat[14];
      Ctrl_SetJT.fnsptp = ((((uint16_t)dw_rxbuf_back.dat[15]) << 8) & 0xff00u) + dw_rxbuf_back.dat[16];
      Ctrl_SetJT.fnrttp = ((((uint16_t)dw_rxbuf_back.dat[17]) << 8) & 0xff00u) + dw_rxbuf_back.dat[18];
      
      Ctrl_SetJT.nqfq = ((((uint16_t)dw_rxbuf_back.dat[19]) << 8) & 0xff00u) + dw_rxbuf_back.dat[20];
      Ctrl_SetJT.nqhitp = ((((uint16_t)dw_rxbuf_back.dat[21]) << 8) & 0xff00u) + dw_rxbuf_back.dat[22];
      Ctrl_SetJT.nqlotp = ((((uint16_t)dw_rxbuf_back.dat[23]) << 8) & 0xff00u) + dw_rxbuf_back.dat[24];
      Ctrl_SetJT.nqrttp = ((((uint16_t)dw_rxbuf_back.dat[25]) << 8) & 0xff00u) + dw_rxbuf_back.dat[26];
      
      Ctrl_SetJT.bjgw = ((((uint16_t)dw_rxbuf_back.dat[27]) << 8) & 0xff00u) + dw_rxbuf_back.dat[28];
      Ctrl_SetJT.bjdw = ((((uint16_t)dw_rxbuf_back.dat[29]) << 8) & 0xff00u) + dw_rxbuf_back.dat[30];
      
      if(memcmp(&Ctrl_SetJT, &Ctrl_ParaJT, sizeof(Ctrl_ParaJTType)) != 0)
      {
        if(Ctr_CheckJTPara(&Ctrl_SetJT) == 0)
        {
          Ctrl_ParaJT = Ctrl_SetJT;
          
          Route_ec_tonet_jt();
          
          sftype = SFLASH_SAVE_CTRJT;
          if(Storage_qhd != NULL)
          {
            xQueueSend(Storage_qhd, &sftype, 0);
          }
        }
      }
    }
  }
  
  vTaskResume(xDwLcdTx_Thd);
}

static void unpack_dl(void)
{
  Storage_SaveType sftype;
  uint8_t i, pos;
  
  vTaskSuspend(xDwLcdTx_Thd);
  
  xQueueReset(Dw_Rx_Qhd);
  
  Lcd_UartSendBuf(dwlcd_get_dl, 7);
  
  if(xQueueReceive(Dw_Rx_Qhd, &dw_rxbuf_back, 5000) == pdTRUE)
  {
    if((dw_rxbuf_back.dat[0] == dwlcd_get_dl[4]) && \
       (dw_rxbuf_back.dat[2] == dwlcd_get_dl[6]) && \
       (dw_rxbuf_back.len == ((dwlcd_get_dl[6]*2)+3)))
    {
      Ctrl_SetDL1 = Ctrl_ParaDL1;
      Ctrl_SetDL2 = Ctrl_ParaDL2;
      
      pos = 3;
      for(i=0; i<CTR_FAN_CHANNEL_ALLNUM; i++)
      {
        Ctrl_SetDL1.fntp[i] = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
        pos += 2;
      }
      
      Ctrl_SetDL2.fnrttp = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      for(i=0; i<CTR_SHL_CHANNEL_ALLNUM; i++)
      {
        Ctrl_SetDL1.sltp[i] = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
        pos += 2;
      }
      
      Ctrl_SetDL2.slrttp = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      for(i=0; i<CTR_NUQ_CHANNEL_ALLNUM; i++)
      {
        Ctrl_SetDL1.nqtp[i] = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
        pos += 2;
      }
      
      Ctrl_SetDL2.nqrttp = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      Ctrl_SetDL2.nqfq = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      for(i=0; i<2; i++)
      {
        Ctrl_SetDL1.vftp[i] = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
        pos += 2;
      }
      
      if(memcmp(&Ctrl_SetDL1, &Ctrl_ParaDL1, sizeof(Ctrl_ParaDL1Type)) != 0)
      {
        if(Ctr_CheckDL1Para(&Ctrl_SetDL1) == 0)
        {
          Ctrl_ParaDL1 = Ctrl_SetDL1;
          
          Route_ec_tonet_dl1();
          
          sftype = SFLASH_SAVE_CTRDL1;
          if(Storage_qhd != NULL)
          {
            xQueueSend(Storage_qhd, &sftype, 0);
          }
        }
      }
      
      if(memcmp(&Ctrl_SetDL2, &Ctrl_ParaDL2, sizeof(Ctrl_ParaDL2Type)) != 0)
      {
        if(Ctr_CheckDL2Para(&Ctrl_SetDL2) == 0)
        {
          Ctrl_ParaDL2 = Ctrl_SetDL2;
          
          Route_ec_tonet_dl2();
          
          sftype = SFLASH_SAVE_CTRDL2;
          if(Storage_qhd != NULL)
          {
            xQueueSend(Storage_qhd, &sftype, 0);
          }
        }
      }
    }
  }
  
  vTaskResume(xDwLcdTx_Thd);
}

static void unpack_sk(void)
{
  Storage_SaveType sftype;
  uint8_t i, pos;
  
  vTaskSuspend(xDwLcdTx_Thd);
  
  xQueueReset(Dw_Rx_Qhd);
  
  Lcd_UartSendBuf(dwlcd_get_sk, 7);
  
  if(xQueueReceive(Dw_Rx_Qhd, &dw_rxbuf_back, 5000) == pdTRUE)
  {
    if((dw_rxbuf_back.dat[0] == dwlcd_get_sk[4]) && \
       (dw_rxbuf_back.dat[2] == dwlcd_get_sk[6]) && \
       (dw_rxbuf_back.len == ((dwlcd_get_sk[6]*2)+3)))
    {
      Ctrl_SetMD = Ctrl_ParaMD;
      Ctrl_SetSK = Ctrl_ParaSK;
      
      Ctrl_SetMD.fnmdsken = ((((uint16_t)dw_rxbuf_back.dat[3]) << 8) & 0xff00u) + dw_rxbuf_back.dat[4];
      Ctrl_SetMD.slmdsken = ((((uint16_t)dw_rxbuf_back.dat[5]) << 8) & 0xff00u) + dw_rxbuf_back.dat[6];
      Ctrl_SetMD.nqmdsken = ((((uint16_t)dw_rxbuf_back.dat[7]) << 8) & 0xff00u) + dw_rxbuf_back.dat[8];
      Ctrl_SetMD.vfmdsken = ((((uint16_t)dw_rxbuf_back.dat[9]) << 8) & 0xff00u) + dw_rxbuf_back.dat[10];
      
      pos = 11;
      for(i=0; i<CTR_FAN_CHANNEL_ALLNUM; i++)
      {
        if(dw_rxbuf_back.dat[pos+1] == 4)
        {
          Ctrl_SetSK.fntd[i] = 0;
        }
        else if(dw_rxbuf_back.dat[pos+1] == 5)
        {
          Ctrl_SetSK.fntd[i] = 1;
        }
        else
        {
          return;
        }
        
        pos += 2;
      }
      
      Ctrl_SetSK.fnen = dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      Ctrl_SetSK.fnkq = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      Ctrl_SetSK.fngb = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      for(i=0; i<CTR_SHL_CHANNEL_ALLNUM; i++)
      {
        if(dw_rxbuf_back.dat[pos+1] == 4)
        {
          Ctrl_SetSK.sltd[i] = 0;
        }
        else if(dw_rxbuf_back.dat[pos+1] == 5)
        {
          Ctrl_SetSK.sltd[i] = 1;
        }
        else
        {
          return;
        }
        
        pos += 2;
      }
      
      Ctrl_SetSK.slen = dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      Ctrl_SetSK.slkq = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      Ctrl_SetSK.slgb = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      for(i=0; i<CTR_NUQ_CHANNEL_ALLNUM; i++)
      {
        if(dw_rxbuf_back.dat[pos+1] == 4)
        {
          Ctrl_SetSK.nqtd[i] = 0;
        }
        else if(dw_rxbuf_back.dat[pos+1] == 5)
        {
          Ctrl_SetSK.nqtd[i] = 1;
        }
        else
        {
          return;
        }
        
        pos += 2;
      }
      
      Ctrl_SetSK.nqen = dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      Ctrl_SetSK.nqkq = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      Ctrl_SetSK.nqgb = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
      pos += 2;
      
      for(i=0; i<CTR_VFD_CHANNEL_ALLNUM; i++)
      {
        Ctrl_SetSK.vftf[i] = dw_rxbuf_back.dat[pos+1];
        pos += 2;
        
        Ctrl_SetSK.vfkq[i] = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
        pos += 2;
        
        Ctrl_SetSK.vfgb[i] = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
        pos += 2;
        
        Ctrl_SetSK.vfjs[i] = ((((uint16_t)dw_rxbuf_back.dat[pos]) << 8) & 0xff00u) + dw_rxbuf_back.dat[pos+1];
        pos += 2;
      }
      
      if(memcmp(&Ctrl_SetMD, &Ctrl_ParaMD, sizeof(Ctrl_ParaMDType)) != 0)
      {
        if(Ctr_CheckMDPara(&Ctrl_SetMD) == 0)
        {
          Ctrl_ParaMD = Ctrl_SetMD;
          
          Route_ec_tonet_md();
          
          sftype = SFLASH_SAVE_CTRMD;
          if(Storage_qhd != NULL)
          {
            xQueueSend(Storage_qhd, &sftype, 0);
          }
        }
      }
      
      if(memcmp(&Ctrl_SetSK, &Ctrl_ParaSK, sizeof(Ctrl_ParaSKType)) != 0)
      {
        if(Ctr_CheckSKPara(&Ctrl_SetSK) == 0)
        {
          Ctrl_ParaSK = Ctrl_SetSK;
          
          Route_ec_tonet_sk();
          
          sftype = SFLASH_SAVE_CTRSK;
          if(Storage_qhd != NULL)
          {
            xQueueSend(Storage_qhd, &sftype, 0);
          }
        }
      }
    }
  }
  
  vTaskResume(xDwLcdTx_Thd);
}

static void unpack_td(void)
{
  Storage_SaveType sftype;
  uint8_t i, pos;
  
  vTaskSuspend(xDwLcdTx_Thd);
  
  xQueueReset(Dw_Rx_Qhd);
  
  Lcd_UartSendBuf(dwlcd_get_td, 7);
  
  if(xQueueReceive(Dw_Rx_Qhd, &dw_rxbuf_back, 5000) == pdTRUE)
  {
    if((dw_rxbuf_back.dat[0] == dwlcd_get_td[4]) && \
       (dw_rxbuf_back.dat[2] == dwlcd_get_td[6]) && \
       (dw_rxbuf_back.len == ((dwlcd_get_td[6]*2)+3)))
    {
      Ctrl_SetTD = Ctrl_ParaTD;
      
      pos = 3;
      
      for(i=0; i<CTR_FAN_CHANNEL_ALLNUM; i++)
      {
        if(dw_rxbuf_back.dat[pos+1] == 0)
        {
          Ctrl_SetTD.fntd[i] = 0;
        }
        else if(dw_rxbuf_back.dat[pos+1] == 1)
        {
          Ctrl_SetTD.fntd[i] = 1;
        }
        else
        {
          goto unpack_td_return;
        }
        
        pos += 2;
      }
      
      for(i=0; i<CTR_SHL_CHANNEL_ALLNUM; i++)
      {
        if(dw_rxbuf_back.dat[pos+1] == 0)
        {
          Ctrl_SetTD.sltd[i] = 0;
        }
        else if(dw_rxbuf_back.dat[pos+1] == 1)
        {
          Ctrl_SetTD.sltd[i] = 1;
        }
        else
        {
          goto unpack_td_return;
        }
        
        pos += 2;
      }
      
      for(i=0; i<CTR_NUQ_CHANNEL_ALLNUM; i++)
      {
        if(dw_rxbuf_back.dat[pos+1] == 0)
        {
          Ctrl_SetTD.nqtd[i] = 0;
        }
        else if(dw_rxbuf_back.dat[pos+1] == 1)
        {
          Ctrl_SetTD.nqtd[i] = 1;
        }
        else
        {
          goto unpack_td_return;
        }
        
        pos += 2;
      }
      
      for(i=0; i<CTR_VFD_CHANNEL_ALLNUM; i++)
      {
        if(dw_rxbuf_back.dat[pos+1] == 0)
        {
          Ctrl_SetTD.vftd[i] = 0;
        }
        else if(dw_rxbuf_back.dat[pos+1] == 1)
        {
          Ctrl_SetTD.vftd[i] = 1;
        }
        else
        {
          goto unpack_td_return;
        }
        
        pos += 2;
      }
      
      if(dw_rxbuf_back.dat[pos+1] == 0)
      {
        Ctrl_SetTD.bjtd = 0;
      }
      else if(dw_rxbuf_back.dat[pos+1] == 1)
      {
        Ctrl_SetTD.bjtd = 1;
      }
      else
      {
        goto unpack_td_return;
      }
        
      if(memcmp(&Ctrl_SetTD, &Ctrl_ParaTD, sizeof(Ctrl_ParaTDType)) != 0)
      {
        if(Ctr_CheckTDPara(&Ctrl_SetTD) == 0)
        {
          Ctrl_ParaTD = Ctrl_SetTD;
          
          Route_ec_tonet_td();
          
          sftype = SFLASH_SAVE_CTRTD;
          if(Storage_qhd != NULL)
          {
            xQueueSend(Storage_qhd, &sftype, 0);
          }
        }
      }
    }
  }

unpack_td_return:

  vTaskResume(xDwLcdTx_Thd);
}

void DW_Send_devinfo(void)
{
  uint8_t len;
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
  p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
	p->dat[3] = 0x82u;
	p->dat[4] = 0x70u;
	p->dat[5] = 0x0Cu;

#if (COLLECTOR_USE_TYPE == COLLECTOR_USE_NTC)
	len = snprintf((char *)(p->dat+6), 32, "%s-%s-N-%s-%d", ROUTE_SELF_DID, Sys_Version.ver, Sys_Version.vfd_ver, SysCfg_RunPara.lora_num);
#elif (COLLECTOR_USE_TYPE == COLLECTOR_USE_LORA)
	len = snprintf((char *)(p->dat+6), 32, "%s-%s-L-%s-%d", ROUTE_SELF_DID, Sys_Version.ver, Sys_Version.vfd_ver, SysCfg_RunPara.lora_num);
#else
	#error must define COLLECTOR_USE_TYPE in bsp.h.
#endif
  
  p->dat[2] = len + (6-3);
  p->len = len+6;
  
	xQueueSend(Dw_Tx_Qhd, p, 0);
  
  vPortFree(p);
}

void DW_Send_ec_mode(void)
{
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
	p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
	p->dat[2] = 5;
	p->dat[3] = 0x82u;
	p->dat[4] = 0x30u;
	p->dat[5] = 0x00u;
  
	p->dat[6] = 0;
  
	switch(Ctrl_ParaMD.mode)
	{
		case CTRL_MODE_MU:
		  p->dat[7] = 5;
			break;
		
		case CTRL_MODE_JT:
		  p->dat[7] = 1;
			break;
		
		case CTRL_MODE_DL:
		  p->dat[7] = 2;
			break;
		
		case CTRL_MODE_SK:
		  p->dat[7] = 3;
			break;
		
		case CTRL_MODE_ZN:
      p->dat[7] = 4;
			break;
		
		default:
			return;
	}
	
	p->len = 8;
  
	xQueueSend(Dw_Tx_Qhd, p, 0);
  
  vPortFree(p);
}

void DW_Send_ec_para_jt(void)
{
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
	p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
	p->dat[2] = 31;
	p->dat[3] = 0x82u;
	p->dat[4] = 0x40u;
	p->dat[5] = 0x00u;
  
	p->dat[6] = (uint8_t)((Ctrl_ParaJT.slhitp & 0xFF00u) >> 8);
	p->dat[7] = (uint8_t)(Ctrl_ParaJT.slhitp & 0x00FFu);
	
  p->dat[8] = (uint8_t)((Ctrl_ParaJT.sllotp & 0xFF00u) >> 8);
	p->dat[9] = (uint8_t)(Ctrl_ParaJT.sllotp & 0x00FFu);
  
  p->dat[10] = (uint8_t)((Ctrl_ParaJT.slrttp & 0xFF00u) >> 8);
	p->dat[11] = (uint8_t)(Ctrl_ParaJT.slrttp & 0x00FFu);
  
  
  p->dat[12] = (uint8_t)((Ctrl_ParaJT.fnhin3 & 0xFF00u) >> 8);
	p->dat[13] = (uint8_t)(Ctrl_ParaJT.fnhin3 & 0x00FFu);
  
  
  p->dat[14] = (uint8_t)((Ctrl_ParaJT.fnhitp & 0xFF00u) >> 8);
	p->dat[15] = (uint8_t)(Ctrl_ParaJT.fnhitp & 0x00FFu);
  
  p->dat[16] = (uint8_t)((Ctrl_ParaJT.fnlotp & 0xFF00u) >> 8);
	p->dat[17] = (uint8_t)(Ctrl_ParaJT.fnlotp & 0x00FFu);
  
  p->dat[18] = (uint8_t)((Ctrl_ParaJT.fnsptp & 0xFF00u) >> 8);
	p->dat[19] = (uint8_t)(Ctrl_ParaJT.fnsptp & 0x00FFu);
  
  p->dat[20] = (uint8_t)((Ctrl_ParaJT.fnrttp & 0xFF00u) >> 8);
	p->dat[21] = (uint8_t)(Ctrl_ParaJT.fnrttp & 0x00FFu);
  
  
  p->dat[22] = 0;
	p->dat[23] = (uint8_t)(Ctrl_ParaJT.nqfq & 0x00FFu);
  
  p->dat[24] = (uint8_t)((Ctrl_ParaJT.nqhitp & 0xFF00u) >> 8);
	p->dat[25] = (uint8_t)(Ctrl_ParaJT.nqhitp & 0x00FFu);
  
  p->dat[26] = (uint8_t)((Ctrl_ParaJT.nqlotp & 0xFF00u) >> 8);
	p->dat[27] = (uint8_t)(Ctrl_ParaJT.nqlotp & 0x00FFu);
  
  p->dat[28] = (uint8_t)((Ctrl_ParaJT.nqrttp & 0xFF00u) >> 8);
	p->dat[29] = (uint8_t)(Ctrl_ParaJT.nqrttp & 0x00FFu);
  
  p->dat[30] = (uint8_t)((Ctrl_ParaJT.bjgw & 0xFF00u) >> 8);
	p->dat[31] = (uint8_t)(Ctrl_ParaJT.bjgw & 0x00FFu);
  
  p->dat[32] = (uint8_t)((Ctrl_ParaJT.bjdw & 0xFF00u) >> 8);
	p->dat[33] = (uint8_t)(Ctrl_ParaJT.bjdw & 0x00FFu);
  
	p->len = 34;
  
  xQueueSend(Dw_Tx_Qhd, p, 0);
  
  vPortFree(p);
}

void DW_Send_ec_para_dl(void)
{
  uint8_t i;
  uint8_t pos;
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
	p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
	p->dat[3] = 0x82u;
	p->dat[4] = 0x50u;
	p->dat[5] = 0x00u;
  
  pos = 6;
  
  for(i=0; i<CTR_FAN_CHANNEL_ALLNUM; i++)
  {
    p->dat[pos] = (uint8_t)((Ctrl_ParaDL1.fntp[i] & 0xFF00u) >> 8);
    p->dat[pos+1] = (uint8_t)(Ctrl_ParaDL1.fntp[i] & 0x00FFu);
    pos += 2;
  }
	
  p->dat[pos] = (uint8_t)((Ctrl_ParaDL2.fnrttp & 0xFF00u) >> 8);
  p->dat[pos+1] = (uint8_t)(Ctrl_ParaDL2.fnrttp & 0x00FFu);
  pos += 2;
  
  for(i=0; i<CTR_SHL_CHANNEL_ALLNUM; i++)
  {
    p->dat[pos] = (uint8_t)((Ctrl_ParaDL1.sltp[i] & 0xFF00u) >> 8);
    p->dat[pos+1] = (uint8_t)(Ctrl_ParaDL1.sltp[i] & 0x00FFu);
    pos += 2;
  }
  
  p->dat[pos] = (uint8_t)((Ctrl_ParaDL2.slrttp & 0xFF00u) >> 8);
  p->dat[pos+1] = (uint8_t)(Ctrl_ParaDL2.slrttp & 0x00FFu);
  pos += 2;
  
  for(i=0; i<CTR_NUQ_CHANNEL_ALLNUM; i++)
  {
    p->dat[pos] = (uint8_t)((Ctrl_ParaDL1.nqtp[i] & 0xFF00u) >> 8);
    p->dat[pos+1] = (uint8_t)(Ctrl_ParaDL1.nqtp[i] & 0x00FFu);
    pos += 2;
  }
  
  p->dat[pos] = (uint8_t)((Ctrl_ParaDL2.nqrttp & 0xFF00u) >> 8);
  p->dat[pos+1] = (uint8_t)(Ctrl_ParaDL2.nqrttp & 0x00FFu);
  pos += 2;
  
  p->dat[pos] = (uint8_t)((Ctrl_ParaDL2.nqfq & 0xFF00u) >> 8);
  p->dat[pos+1] = (uint8_t)(Ctrl_ParaDL2.nqfq & 0x00FFu);
  pos += 2;
  
  /* 变频器只设置2个 */
  for(i=0; i<2; i++)
  {
    p->dat[pos] = (uint8_t)((Ctrl_ParaDL1.vftp[i] & 0xFF00u) >> 8);
    p->dat[pos+1] = (uint8_t)(Ctrl_ParaDL1.vftp[i] & 0x00FFu);
    pos += 2;
  }
  
  p->dat[2] = pos-3;
  
	p->len = pos;
  
  xQueueSend(Dw_Tx_Qhd, p, 0);

  vPortFree(p);
}

void DW_Send_ec_para_sk(void)
{
  uint8_t i;
  uint8_t pos;
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
	p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
	p->dat[3] = 0x82u;
	p->dat[4] = 0x60u;
	p->dat[5] = 0x01u;

  p->dat[6] = 0;
  p->dat[7] = Ctrl_ParaMD.fnmdsken;
  
  p->dat[8] = 0;
  p->dat[9] = Ctrl_ParaMD.slmdsken;
  
  p->dat[10] = 0;
  p->dat[11] = Ctrl_ParaMD.nqmdsken;
  
  p->dat[12] = 0;
  p->dat[13] = Ctrl_ParaMD.vfmdsken;
  
  pos = 14;
  
  for(i=0; i<CTR_FAN_CHANNEL_ALLNUM; i++)
  {
    p->dat[pos] = 0;
    if(Ctrl_ParaSK.fntd[i] != 0)
    {
      p->dat[pos+1] = 5;
    }
    else
    {
      p->dat[pos+1] = 4;
    }
    pos += 2;
  }
	
  p->dat[pos] = 0;
  p->dat[pos+1] = Ctrl_ParaSK.fnen;
  pos += 2;
  
  p->dat[pos] = (uint8_t)((Ctrl_ParaSK.fnkq & 0xFF00u) >> 8);
  p->dat[pos+1] = (uint8_t)(Ctrl_ParaSK.fnkq & 0x00FFu);
  pos += 2;
  
  p->dat[pos] = (uint8_t)((Ctrl_ParaSK.fngb & 0xFF00u) >> 8);
  p->dat[pos+1] = (uint8_t)(Ctrl_ParaSK.fngb & 0x00FFu);
  pos += 2;
  
  
  
  for(i=0; i<CTR_SHL_CHANNEL_ALLNUM; i++)
  {
    p->dat[pos] = 0;
    if(Ctrl_ParaSK.sltd[i] != 0)
    {
      p->dat[pos+1] = 5;
    }
    else
    {
      p->dat[pos+1] = 4;
    }
    pos += 2;
  }
  
  p->dat[pos] = 0;
  p->dat[pos+1] = Ctrl_ParaSK.slen;
  pos += 2;
  
  p->dat[pos] = (uint8_t)((Ctrl_ParaSK.slkq & 0xFF00u) >> 8);
  p->dat[pos+1] = (uint8_t)(Ctrl_ParaSK.slkq & 0x00FFu);
  pos += 2;
  
  p->dat[pos] = (uint8_t)((Ctrl_ParaSK.slgb & 0xFF00u) >> 8);
  p->dat[pos+1] = (uint8_t)(Ctrl_ParaSK.slgb & 0x00FFu);
  pos += 2;
  
  
  for(i=0; i<CTR_NUQ_CHANNEL_ALLNUM; i++)
  {
    p->dat[pos] = 0;
    if(Ctrl_ParaSK.nqtd[i] != 0)
    {
      p->dat[pos+1] = 5;
    }
    else
    {
      p->dat[pos+1] = 4;
    }
    pos += 2;
  }  

  p->dat[pos] = 0;
  p->dat[pos+1] = Ctrl_ParaSK.nqen;
  pos += 2;
  
  p->dat[pos] = (uint8_t)((Ctrl_ParaSK.nqkq & 0xFF00u) >> 8);
  p->dat[pos+1] = (uint8_t)(Ctrl_ParaSK.nqkq & 0x00FFu);
  pos += 2;
  
  p->dat[pos] = (uint8_t)((Ctrl_ParaSK.nqgb & 0xFF00u) >> 8);
  p->dat[pos+1] = (uint8_t)(Ctrl_ParaSK.nqgb & 0x00FFu);
  pos += 2;
  
  /* 变频器只设置2个 */
  for(i=0; i<CTR_VFD_CHANNEL_ALLNUM; i++)
  {
    p->dat[pos] = 0;
    p->dat[pos+1] = Ctrl_ParaSK.vftf[i];
    
    p->dat[pos+2] = (uint8_t)((Ctrl_ParaSK.vfkq[i] & 0xFF00u) >> 8);
    p->dat[pos+3] = (uint8_t)(Ctrl_ParaSK.vfkq[i] & 0x00FFu);
    
    p->dat[pos+4] = (uint8_t)((Ctrl_ParaSK.vfgb[i] & 0xFF00u) >> 8);
    p->dat[pos+5] = (uint8_t)(Ctrl_ParaSK.vfgb[i] & 0x00FFu);
    
    p->dat[pos+6] = 0;
    p->dat[pos+7] = Ctrl_ParaSK.vfjs[i];
    
    pos += 8;
  }
  
  p->dat[2] = pos-3;
  
	p->len = pos;
  
  xQueueSend(Dw_Tx_Qhd, p, 0);
  
  vPortFree(p);
}

void DW_Send_ec_para_td(void)
{
  uint8_t i, pos;
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
	p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
	p->dat[3] = 0x82u;
	p->dat[4] = 0x21u;
	p->dat[5] = 0x00u;
  
  pos = 6;
  
  for(i=0; i<CTR_FAN_CHANNEL_ALLNUM; i++)
  {
    p->dat[pos++] = 0;
    
    if(Ctrl_ParaTD.fntd[i] != 0)
    {
      p->dat[pos] = 1;
    }
    else
    {
      p->dat[pos] = 0;
    }
    pos++;
  }
  
  for(i=0; i<CTR_SHL_CHANNEL_ALLNUM; i++)
  {
    p->dat[pos++] = 0;

    if(Ctrl_ParaTD.sltd[i] != 0)
    {
      p->dat[pos] = 1;
    }
    else
    {
      p->dat[pos] = 0;
    }
    pos++;
  }
  
  for(i=0; i<CTR_NUQ_CHANNEL_ALLNUM; i++)
  {
    p->dat[pos++] = 0;

    if(Ctrl_ParaTD.nqtd[i] != 0)
    {
      p->dat[pos] = 1;
    }
    else
    {
      p->dat[pos] = 0;
    }
    pos++;
  }
  
  for(i=0; i<CTR_VFD_CHANNEL_ALLNUM; i++)
  {
    p->dat[pos++] = 0;

    if(Ctrl_ParaTD.vftd[i] != 0)
    {
      p->dat[pos] = 1;
    }
    else
    {
      p->dat[pos] = 0;
    }
    pos++;
  }
  
	p->dat[pos++] = 0;
	if(Ctrl_ParaTD.bjtd != 0)
  {
    p->dat[pos++] = 1;
  }
  else
  {
    p->dat[pos++] = 0;
  }
  
  p->dat[2] = pos-3;
	p->len = pos;
  
  xQueueSend(Dw_Tx_Qhd, p, 0);
  
  vPortFree(p);
}

void DW_Send_rilcl(void)
{
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
  p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
	p->dat[2] = 7;
	p->dat[3] = 0x82u;
	p->dat[4] = 0x10u;
	p->dat[5] = 0x06u;
  
  p->dat[6] = (uint8_t)((SysRil_RunPara.ril& 0xFF00u) >> 8);
	p->dat[7] = (uint8_t)(SysRil_RunPara.ril & 0x00FFu);
	
	p->dat[8] = (uint8_t)((SysRil_RunPara.clnum & 0xFF00u) >> 8);
	p->dat[9] = (uint8_t)(SysRil_RunPara.clnum & 0x00FFu);
	
	p->len = 10;
  
  xQueueSend(Dw_Tx_Qhd, p, 0);
  
  vPortFree(p);
}

void DW_Send_temp(void)
{
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
	p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
	p->dat[2] = 15;
	p->dat[3] = 0x82u;
	p->dat[4] = 0x10u;
	p->dat[5] = 0x00u;
	
	/* 1000 */
	p->dat[6] = (uint8_t)((Collector_LocalData.average.temperature & 0xFF00u) >> 8);
	p->dat[7] = (uint8_t)(Collector_LocalData.average.temperature & 0x00FFu);
	
	p->dat[8] = (uint8_t)((Collector_LocalData.average.humidity & 0xFF00u) >> 8);
	p->dat[9] = (uint8_t)(Collector_LocalData.average.humidity & 0x00FFu);
	
	p->dat[10] = (uint8_t)((Collector_LocalData.average.nh3 & 0xFF00u) >> 8);
	p->dat[11] = (uint8_t)(Collector_LocalData.average.nh3 & 0x00FFu);
	
	p->dat[12] = (uint8_t)((Collector_LocalData.average.tvoc & 0xFF00u) >> 8);
	p->dat[13] = (uint8_t)(Collector_LocalData.average.tvoc & 0x00FFu);
	
	p->dat[14] = (uint8_t)((Collector_LocalData.average.co2 & 0xFF00u) >> 8);
	p->dat[15] = (uint8_t)(Collector_LocalData.average.co2 & 0x00FFu);
	
	p->dat[16] = (uint8_t)((Collector_LocalData.average.pressure & 0xFF00u) >> 8);
	p->dat[17] = (uint8_t)(Collector_LocalData.average.pressure & 0x00FFu);
	
	p->len = 18;
  
  xQueueSend(Dw_Tx_Qhd, p, 0);
  
  vPortFree(p);
}

void DW_Send_nodetemp(void)
{	
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
	p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
#if(COLLECTOR_USE_TYPE == COLLECTOR_USE_LORA)
	p->dat[2] = 75;
#else
  p->dat[2] = 51;
#endif
	p->dat[3] = 0x82u;
	p->dat[4] = 0x20u;
	p->dat[5] = 0x00u;
	
	/* 1000 */
	p->dat[6] = (uint8_t)((Collector_LocalData.device[0].temperature & 0xFF00u) >> 8);
	p->dat[7] = (uint8_t)(Collector_LocalData.device[0].temperature & 0x00FFu);
	
	p->dat[8] = (uint8_t)((Collector_LocalData.device[0].humidity & 0xFF00u) >> 8);
	p->dat[9] = (uint8_t)(Collector_LocalData.device[0].humidity & 0x00FFu);
	
	p->dat[10] = (uint8_t)((Collector_LocalData.device[0].nh3 & 0xFF00u) >> 8);
	p->dat[11] = (uint8_t)(Collector_LocalData.device[0].nh3 & 0x00FFu);
	
	p->dat[12] = (uint8_t)((Collector_LocalData.device[0].tvoc & 0xFF00u) >> 8);
	p->dat[13] = (uint8_t)(Collector_LocalData.device[0].tvoc & 0x00FFu);
	
	p->dat[14] = (uint8_t)((Collector_LocalData.device[0].co2 & 0xFF00u) >> 8);
	p->dat[15] = (uint8_t)(Collector_LocalData.device[0].co2 & 0x00FFu);
	
  p->dat[16] = (uint8_t)((Collector_LocalData.device[0].pressure & 0xFF00u) >> 8);
	p->dat[17] = (uint8_t)(Collector_LocalData.device[0].pressure & 0x00FFu);
	
  
	p->dat[18] = (uint8_t)((Collector_LocalData.device[1].temperature & 0xFF00u) >> 8);
	p->dat[19] = (uint8_t)(Collector_LocalData.device[1].temperature & 0x00FFu);
	
	p->dat[20] = (uint8_t)((Collector_LocalData.device[1].humidity & 0xFF00u) >> 8);
	p->dat[21] = (uint8_t)(Collector_LocalData.device[1].humidity & 0x00FFu);
	
	p->dat[22] = (uint8_t)((Collector_LocalData.device[1].nh3 & 0xFF00u) >> 8);
	p->dat[23] = (uint8_t)(Collector_LocalData.device[1].nh3 & 0x00FFu);
	
	p->dat[24] = (uint8_t)((Collector_LocalData.device[1].tvoc & 0xFF00u) >> 8);
	p->dat[25] = (uint8_t)(Collector_LocalData.device[1].tvoc & 0x00FFu);
	
	p->dat[26] = (uint8_t)((Collector_LocalData.device[1].co2 & 0xFF00u) >> 8);
	p->dat[27] = (uint8_t)(Collector_LocalData.device[1].co2 & 0x00FFu);
	
	p->dat[28] = (uint8_t)((Collector_LocalData.device[1].pressure & 0xFF00u) >> 8);
	p->dat[29] = (uint8_t)(Collector_LocalData.device[1].pressure & 0x00FFu);
  
  
	p->dat[30] = (uint8_t)((Collector_LocalData.device[2].temperature & 0xFF00u) >> 8);
	p->dat[31] = (uint8_t)(Collector_LocalData.device[2].temperature & 0x00FFu);
	
	p->dat[32] = (uint8_t)((Collector_LocalData.device[2].humidity & 0xFF00u) >> 8);
	p->dat[33] = (uint8_t)(Collector_LocalData.device[2].humidity & 0x00FFu);
	
	p->dat[34] = (uint8_t)((Collector_LocalData.device[2].nh3 & 0xFF00u) >> 8);
	p->dat[35] = (uint8_t)(Collector_LocalData.device[2].nh3 & 0x00FFu);
	
	p->dat[36] = (uint8_t)((Collector_LocalData.device[2].tvoc & 0xFF00u) >> 8);
	p->dat[37] = (uint8_t)(Collector_LocalData.device[2].tvoc & 0x00FFu);
	
	p->dat[38] = (uint8_t)((Collector_LocalData.device[2].co2 & 0xFF00u) >> 8);
	p->dat[39] = (uint8_t)(Collector_LocalData.device[2].co2 & 0x00FFu);
	
  p->dat[40] = (uint8_t)((Collector_LocalData.device[2].pressure & 0xFF00u) >> 8);
	p->dat[41] = (uint8_t)(Collector_LocalData.device[2].pressure & 0x00FFu);
  
	
	p->dat[42] = (uint8_t)((Collector_LocalData.device[3].temperature & 0xFF00u) >> 8);
	p->dat[43] = (uint8_t)(Collector_LocalData.device[3].temperature & 0x00FFu);
	
	p->dat[44] = (uint8_t)((Collector_LocalData.device[3].humidity & 0xFF00u) >> 8);
	p->dat[45] = (uint8_t)(Collector_LocalData.device[3].humidity & 0x00FFu);
	
	p->dat[46] = (uint8_t)((Collector_LocalData.device[3].nh3 & 0xFF00u) >> 8);
	p->dat[47] = (uint8_t)(Collector_LocalData.device[3].nh3 & 0x00FFu);
	
	p->dat[48] = (uint8_t)((Collector_LocalData.device[3].tvoc & 0xFF00u) >> 8);
	p->dat[49] = (uint8_t)(Collector_LocalData.device[3].tvoc & 0x00FFu);
	
	p->dat[50] = (uint8_t)((Collector_LocalData.device[3].co2 & 0xFF00u) >> 8);
	p->dat[51] = (uint8_t)(Collector_LocalData.device[3].co2 & 0x00FFu);
	
  p->dat[52] = (uint8_t)((Collector_LocalData.device[3].pressure & 0xFF00u) >> 8);
	p->dat[53] = (uint8_t)(Collector_LocalData.device[3].pressure & 0x00FFu);
  
#if(COLLECTOR_USE_TYPE == COLLECTOR_USE_LORA)
	p->dat[54] = (uint8_t)((Collector_LocalData.device[4].temperature & 0xFF00u) >> 8);
	p->dat[55] = (uint8_t)(Collector_LocalData.device[4].temperature & 0x00FFu);
	
	p->dat[56] = (uint8_t)((Collector_LocalData.device[4].humidity & 0xFF00u) >> 8);
	p->dat[57] = (uint8_t)(Collector_LocalData.device[4].humidity & 0x00FFu);
	
	p->dat[58] = (uint8_t)((Collector_LocalData.device[4].nh3 & 0xFF00u) >> 8);
	p->dat[59] = (uint8_t)(Collector_LocalData.device[4].nh3 & 0x00FFu);
	
	p->dat[60] = (uint8_t)((Collector_LocalData.device[4].tvoc & 0xFF00u) >> 8);
	p->dat[61] = (uint8_t)(Collector_LocalData.device[4].tvoc & 0x00FFu);
	
	p->dat[62] = (uint8_t)((Collector_LocalData.device[4].co2 & 0xFF00u) >> 8);
	p->dat[63] = (uint8_t)(Collector_LocalData.device[4].co2 & 0x00FFu);
	
  p->dat[64] = (uint8_t)((Collector_LocalData.device[4].pressure & 0xFF00u) >> 8);
	p->dat[65] = (uint8_t)(Collector_LocalData.device[4].pressure & 0x00FFu);
  
	
	p->dat[66] = (uint8_t)((Collector_LocalData.device[5].temperature & 0xFF00u) >> 8);
	p->dat[67] = (uint8_t)(Collector_LocalData.device[5].temperature & 0x00FFu);
	
	p->dat[68] = (uint8_t)((Collector_LocalData.device[5].humidity & 0xFF00u) >> 8);
	p->dat[69] = (uint8_t)(Collector_LocalData.device[5].humidity & 0x00FFu);
	
	p->dat[70] = (uint8_t)((Collector_LocalData.device[5].nh3 & 0xFF00u) >> 8);
	p->dat[71] = (uint8_t)(Collector_LocalData.device[5].nh3 & 0x00FFu);
	
	p->dat[72] = (uint8_t)((Collector_LocalData.device[5].tvoc & 0xFF00u) >> 8);
	p->dat[73] = (uint8_t)(Collector_LocalData.device[5].tvoc & 0x00FFu);
	
	p->dat[74] = (uint8_t)((Collector_LocalData.device[5].co2 & 0xFF00u) >> 8);
	p->dat[75] = (uint8_t)(Collector_LocalData.device[5].co2 & 0x00FFu);
	
  p->dat[76] = (uint8_t)((Collector_LocalData.device[5].pressure & 0xFF00u) >> 8);
	p->dat[77] = (uint8_t)(Collector_LocalData.device[5].pressure & 0x00FFu);

	p->len = 78;
#else
	p->len = 54;
#endif  
  xQueueSend(Dw_Tx_Qhd, p, 0);

  vPortFree(p);
}


void DW_Send_bpq(void)
{
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
	p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
	p->dat[2] = 7;
	p->dat[3] = 0x82u;
	p->dat[4] = 0x10u;
	p->dat[5] = 0x08u;
	
	/* 1000 */
	p->dat[6] = (uint8_t)((Ctrl_StateVF.vfjs[0] & 0xFF00u) >> 8);
	p->dat[7] = (uint8_t)(Ctrl_StateVF.vfjs[0] & 0x00FFu);
	
	p->dat[8] = (uint8_t)((Ctrl_StateVF.vfjs[1] & 0xFF00u) >> 8);
	p->dat[9] = (uint8_t)(Ctrl_StateVF.vfjs[1] & 0x00FFu);
	
	p->len = 10;
  
  xQueueSend(Dw_Tx_Qhd, p, 0);
  
  vPortFree(p);
}

void DW_Send_devlist(void)
{
  uint8_t i, len;
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
	p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
	p->dat[3] = 0x82u;
	p->dat[4] = 0x12u;
	p->dat[5] = 0x00u;
	
  Route_ConvertToTopolist(&Route_LocalTopoList);
  
  len = 6;
  for(i=0; ((i<Route_LocalTopoList.topo_num) && (i<10)); i++)
  {
    len += snprintf((char *)(p->dat+len), ROUTE_DID_STRLEN+2, "%s,", Route_LocalTopoList.dids[i]);
  }

	p->len = len;
  p->dat[2] = len - 3;
  xQueueSend(Dw_Tx_Qhd, p, 0);
  
  vPortFree(p);
}

void DW_Send_changeid(void)
{
  uint8_t len;
  Dw_TxBufType *p = NULL;
  
  if(Dw_Tx_Qhd == NULL)
  {
    return;
  }
  
  p = (Dw_TxBufType *)pvPortMalloc(sizeof(Dw_TxBufType));
  if(p == NULL)
  {
    return;
  }
  
	p->dat[0] = HEADER1;
	p->dat[1] = HEADER2;
	p->dat[3] = 0x82u;
	p->dat[4] = 0x11u;
	p->dat[5] = 0x00u;
	
  p->dat[6] = 0;
  p->dat[7] = SysCfg_RunPara.lora_num;
  
  len = 8;
  len += snprintf((char *)(p->dat+8), 12, "%s", ROUTE_SELF_DID+2);
  
  p->len = len;
  p->dat[2] = len - 3;
  
  xQueueSend(Dw_Tx_Qhd, p, 0);
  
  vPortFree(p);
}

void vDwlcdRecv_Task( void * pvParameters )
{
  uint16_t addr, dat;
  
	dwlcd_init();
	
	xDwLcdTx_Thd = xTaskCreateStatic(vDwlcdSend_Task, "dwTx", DWLCDTX_STACK_SIZE, (void *)1,
																   DWLCDTX_TSK_PRIORITY, xDwLcdTx_Stk, &xDwLcdTx_Tcb);
	while(1)
	{
		if(xQueueReceive(Dw_Rx_Qhd, &dw_rxbuf_back, portMAX_DELAY) == pdTRUE)
		{
      addr = ((((uint16_t)dw_rxbuf_back.dat[0]) << 8) & 0xff00u) + dw_rxbuf_back.dat[1];
			dat  = ((((uint16_t)dw_rxbuf_back.dat[3]) << 8) & 0xff00u) + dw_rxbuf_back.dat[4];
      
      switch(addr)
      {
        case 0x1F00u:
          unpack_devsync();
          break;
        
        case 0x1F01u:
          unpack_devinfo();
          break;
        
        case 0x3000u:
          unpack_md(dat);
          break;
        
        case 0x3001u:
          unpack_sync();
          break;
        
        case 0x3002u:
          unpack_jt();
          break;
        
        case 0x3003u:
          unpack_dl();
          break;
        
        case 0x3004u:
          unpack_sk();
          break;
        
        case 0x3005u:
          unpack_td();
          break;
        
        default:
          break;
      }
    }
	}
}

void vDwlcdSend_Task( void * pvParameters )
{
	while(1)
	{
		if(xQueueReceive(Dw_Tx_Qhd, &dw_txbuf_back, portMAX_DELAY) == pdTRUE)
		{
			Lcd_UartSendBuf(dw_txbuf_back.dat, dw_txbuf_back.len);
		}
		vTaskDelay(25);
	}
}

